package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.activity.tonbing.config.TonBingConfig;
import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tonbing.TonBingModule;
import com.yanqu.road.server.gameplayer.module.activity.tonbingchannel.ChannelTonBingModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

/**
 * 使用后添加巅峰统兵红包
 */
public class UseAddTonBingRedPacket extends BaseUseGoods {

	public UseAddTonBingRedPacket(GamePlayer player, GoodsInfo goodsInfo, long param) {
		super(player, goodsInfo, param);
	}

	@Override
	int checkUse(long count) {
		// 检测是否在有效期
		TonBingConfig config = TonBingMgr.getConfig();
		ChannelTonBingConfig cconfig = ChannelTonBingMgr.getConfig();
		if((config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()))
				&& (cconfig == null || !ActivityMgr.activityInShowTime(cconfig.getActivityInfo()))){
			return GameErrorCode.E_ACTIVITY_NO_FOUND;
		}
		if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo()) && !config.inEnterActivityTime()) {
			return GameErrorCode.E_TON_BING_NO_OPERATE_TIME;
		}
		if (cconfig != null && ActivityMgr.activityInShowTime(cconfig.getActivityInfo()) && !cconfig.inEnterActivityTime()) {
			return GameErrorCode.E_TON_BING_NO_OPERATE_TIME;
		}

		if (cconfig != null) {
			if (!ChannelTonBingMgr.isAble(player.getUserId())) {//没资格不让用
				return GameErrorCode.E_TON_BING_NO_ABLE;
			}
		}

		return 0;
	}

	@Override
	UseGoodsResult executeUse(long count) {
		UseGoodsResult result = new UseGoodsResult();
		TonBingConfig config = TonBingMgr.getConfig();
		ChannelTonBingConfig cconfig = ChannelTonBingMgr.getConfig();
		if(config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())){
			player.getModule(TonBingModule.class).addCrossRedPacket(goodsInfo,count);
		}
		if(cconfig != null && ActivityMgr.activityInShowTime(cconfig.getActivityInfo())){
			//跨域统兵一起用吧我日
			player.getModule(ChannelTonBingModule.class).addCrossRedPacket(goodsInfo,count);
		}
		result.setReward("");
		return result;
	}
}
